<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/3 0003
 * Time: 14:54
 */

namespace App\Models\ActivityDelivery;

use Illuminate\Http\Request;

class ActivityDeliveryService
{
    private $activityDeliveryRepository;

    public function __construct(ActivityDeliveryRepository $activityDeliveryRepository)
    {
        $this->activityDeliveryRepository = $activityDeliveryRepository;
    }

    /**
     * 新建投放
     *
     * @param $data
     */
    public function create($data)
    {
        $activityDelivery = new ActivityDelivery($data);

        return $this->activityDeliveryRepository->create($activityDelivery);
    }

    /**
     * @param Request $request
     *
     * @return array
     */
    public function getActivityDeliveryListByPage($request, $user_id='')
    {
        $start = (int)$request->query->get('offset', 0);
        $limit = (int)$request->query->get('limit', 10);
        $keyword = $request->query->get('search');
        $sort = $request->query->get('sort', 'created_at');
        $order = $request->query->get('order', 'desc');
        $sortArray = [[$sort, $order]];
        $conditions = [
            ['user_id',$user_id],
            ['group_code', session('groupcode')],
        ];
        if (!is_null($keyword)) {
            $conditions[] = ['name', 'like', '%' . $keyword . '%'];
        }

        $list = $this->activityDeliveryRepository->findByConditionsPage($conditions, $sortArray, $start, $limit);

        $list = $list->toArray();
        $dataList = [];
        foreach ($list as $key => $val) {
            $data = [];
            $data['id'] = isset($val['id']) ? $val['id'] : '';
            $data['point_id'] = isset($val['point_id']) ? $val['point_id'] : '';
            $data['name'] = isset($val['name']) ? $val['name'] : '';
            $data['platform'] = isset($val['platform']) ? $val['platform'] : '';
            $data['type'] = isset($val['type']) ? $val['type'] : '';
            $data['status'] = isset($val['status']) ? $val['status'] : '';
            $data['created_at'] = isset($val['created_at']) ? $val['created_at'] : '';
            $data['updated_at'] = isset($val['updated_at']) ? $val['updated_at'] : '';
            $data['act_id'] = isset($val['act_id']) ? $val['act_id'] : '';
            $data['act_name'] = isset($val['act_name']) ? $val['act_name'] : '';
            $data['detail_id'] = isset($val['id']) ? $val['id'] : '';
            $data['img_url'] = isset($val['img_url']) ? $val['img_url'] : '';
            $data['begin_time'] = isset($val['begin_time']) ? date('Y-m-d H:i:s', $val['begin_time']) : '';
            $data['end_time'] = isset($val['end_time']) ? date('Y-m-d H:i:s', $val['end_time']) : '';
            $data['target'] = isset($val['target']) ? $val['target'] : '';
            $data['point_name'] = isset($val['activity_points']['point_name']) ? $val['activity_points']['point_name'] : '';

            $dataList[] = $data;
        }

        $total = $this->activityDeliveryRepository->countByConditions($conditions);

        return ['total' => $total, 'rows' => $dataList];
    }

    public function saveActivityDeliveryById($updates, $id)
    {
        return $this->activityDeliveryRepository->update($updates, ['id' => $id]);
    }

    public function deleteActivityDeliveries($ids)
    {
        return $this->activityDeliveryRepository->deleteByIds($ids);
    }

    public function getActivityDeliveryById($id)
    {
        return $this->activityDeliveryRepository->getActivityDeliveryById($id);
    }

    public function getActivityDeliveryAndDetailsById($id)
    {
        return $this->activityDeliveryRepository->getActivityDeliveryAndDetailsById($id);
    }

    public function createDeliveryAndDetail($activityDeliveryData)
    {
        return $this->activityDeliveryRepository->createDeliveryAndDetail($activityDeliveryData);
    }

    public function modifyDeliveryAndDetail($activityDeliveryData, $id)
    {
        return $this->activityDeliveryRepository->modifyDeliveryAndDetail($activityDeliveryData, $id);
    }

    public function getAvailablePoints($point_id='')
    {
        return $this->activityDeliveryRepository->getAvailablePoints($point_id);
    }
}
